#!/usr/bin/perl

#const

#token type
my $TYPE_UNKNOW = -1;
my $TYPE_MACRO = 0;
my $TYPE_CLASS = 1;
my $TYPE_STRUCT = 2;
my $TYPE_ENUM = 3;

#ret
my $RET_ERROR = -1;
my $RET_TOKEN = 0;
my $RET_BLANK = 1;
my $RET_COMMENT = 2;

#key and del
my $COMMENT_M_B="/*";
my $COMMENT_M_E="*/";
my $COMMENT_S_B="//";
my $COMMENT_D="/";
my $COMMENT_S_E="\n";
my $TOKEN_KEY_MACRO="#define";
my $TOKEN_KEY_CLASS="class";
my $TOKEN_KEY_STRUCT="struct";
my $TOKEN_KEY_ENUM="enum";
my $TOKEN_KEY_INCLUDE="#include";
my $DEL_CLASS_B="{";
my $DEL_CLASS_E="}";
my $DEL_MEMBER=";";
my $DEL_FUNC_B="(";
my $DEL_FUNC_B=")";
my $DEL_PARA = ",";
my $DEL_STR = "\"";

my $header=@ARGV[0];
my $src=@ARGV[1];

my $header_data;

#sub
sub IsBlank($)
{
	if($_[0] =~ /^[\s\r\n]$/)
	{
		return 1;
	}

	return 0;
}

sub SkipComment(\$$$)
{
	my $data = $_[0];
	my $begin = $_[1];
	my $len = $_[2];
	my $flag = 0;

	my $end = $begin;
	my $ret = $RET_TOKEN;
	if(index($$data,$COMMENT_M_B,$begin) == $begin)
	{
		$end = index($$data,$COMMENT_M_E,$begin+length($COMMENT_M_B));
		if($end < 0)
		{
			$end = $len;
		}
		else
		{
			$end += length($COMMENT_M_E);
		}
	}
	elsif(index($$data,$COMMENT_S_B,$begin) == $begin)
	{
		$end = index($$data,$COMMENT_S_E,$begin+length($COMMENT_M_B));
                if($end < 0)
                {
                        $end = $len;
                }
		else
		{
			$end += length($COMMENT_S_E);
		}
	}

	if($end + 1 <= $len)
        {
		if(&IsBlank(substr($$data,$end,1)))
		{
			$ret = $RET_BLANK;
		}
        }

	if($end + length($COMMENT_M_B) <= $len)
	{
		if(index($$data,$COMMENT_M_B,$end) == $end)
                {
                        $ret = $RET_COMMENT;
                }
	}

	if($end + length($COMMENT_S_B) <= $len)
	{
		if(index($$data,$COMMENT_S_B,$end) == $end)
                {
                        $ret = $RET_COMMENT;
                }
	}

	return ($ret,$end);
}

sub SkipBlank(\$$$)
{
	my $data = $_[0];
        my $begin = $_[1];
	my $len = $_[2];

	my $end = $begin;
	my $ret = $RET_TOKEN;
	my $i=0;
	for($i=$begin;$i<$len;$i++)
	{
		my $char = substr($$data,$i,1);
		if(!&IsBlank($char))
		{
			last;
		}
	}

	$end = $i;
	
	if($end + length($COMMENT_M_B) <= $len)
        {
                if(index($$data,$COMMENT_M_B,$end) == $end)
                {
                        $ret = $RET_COMMENT;
                }
        }

        if($end + length($COMMENT_S_B) <= $len)
        {
                if(index($$data,$COMMENT_S_B,$end) == $end)
                {
                        $ret = $RET_COMMENT;
                }
        }
	
	return ($ret,$end);
}

sub SkipCommentBlank(\$$$)
{
	my $data = $_[0];
        my $begin = $_[1];
        my $len = $_[2];

	my $end = $begin;
	my $ret = RET_TOKEN;

	($ret,$end) = &SkipBlank($data,$begin,$len);

	while($ret != $RET_TOKEN && $end < $len)
	{
		if($ret == $RET_BLANK)
		{
			($ret,$end) = &SkipBlank($data,$end,$len);
		}
		elsif($ret == $RET_COMMENT)
		{
			($ret,$end) = &SkipComment($data,$end,$len);
		}
	}

	return ($ret,$end);
}

sub GetToken(\$\$$)
{
	my $data = $_[0];
	my $pos = $_[1];
	my $len = $_[2];
	my $begin=$$pos;
	my $end=$$pos;
	my $type=$TYPE_UNKNOW;
	my $is_in_comment=0;
	my $token_name;
	my $is_in_token=0;
	my $result = 0;

	if($$pos>= $len)
	{
		$result = -1;
	}
	else
	{
		$result = 0;
	}

	$$pos++;

	return ($result,$token_type,$token_name,$begin,$end);
}

#main routine
#read header data
open HEADER,"<",$header or die "can't open file [$header] due to:$!";

while(<HEADER>)
{
	$header_data = $header_data.$_;
}

close HEADER;

#parse header data
my $pos=0;
my $len=length($header_data);
my %token_hash;

=cut;
do
{
	($result,$token_type,$token_name,$begin,$end) = &GetToken(\$header_data,\$pos,$len);
	my $rec = {
		token_type => $token_type,
		begin => $begin,
		end => $end,
	};

	if($result == 0)
	{
		$token_hash{$token_name} = $rec;
	}

}while($result == 0);
=cut;

($res,$end) = &SkipCommentBlank(\$header_data,0,$len);
printf("%d %d\n",$res,$end);


#find class declare(r)
#read source data
#build function map
#find function

